package com.jic.manager.controller.manage.conf;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.CmsTpage;
import com.jic.conf.request.CmsTpageRequest;
import com.jic.conf.response.CmsModuleResponse;
import com.jic.conf.response.CmsTpageResponse;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.service.CmsModuleService;
import com.jic.manager.service.CmsTpageService;
import com.jic.manager.utils.BeanUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用于模版管理服务
 * @author ztq
 *2020-03-07
 */
@Slf4j
@RestController
@RequestMapping("/cmsTpage")
public class CmsTPageController {

	@Autowired
	private CmsTpageService cmsTpageService;
	
	@Autowired
	private CmsModuleService cmsModuleService;

	@ApiOperation(value = "分页查询", notes = "分页查询信息")
	@ResponseBody
	@RequestMapping(value = "cmsTpageQquery", method = RequestMethod.POST)
	public RestResult<PageResult<CmsTpageResponse>> queryPage(@RequestBody CmsTpageRequest cmsTpageRequest) {

		log.info("开始调用分页，{}", JSON.toJSONString(cmsTpageRequest));

		Page page = new Page();
		page.setLimit(cmsTpageRequest.getLimit());
		page.setStart(cmsTpageRequest.getStart());

		CmsTpage cmsTtage = BeanUtil.copy(cmsTpageRequest, CmsTpage.class);
		cmsTtage.setDeleteFlag(0);
		PageResult<CmsTpage> pageRresult = cmsTpageService.queryPages(cmsTtage, page);

		List<CmsTpageResponse> cmsPageResponseList = pageRresult.getRows().stream().map(temp -> {
			CmsTpageResponse cmsPageResponse = BeanUtil.copy(temp, CmsTpageResponse.class);
			return cmsPageResponse;
		}).filter(item -> item != null).collect(Collectors.toList());

		PageResult<CmsTpageResponse> result = new PageResult<>();
		result.setRows(cmsPageResponseList);
		result.setTotal(pageRresult.getTotal());

		return RestResult.success(result);
	}

	

	/**
	 * 
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@ApiOperation(value = "模版删除", notes = "模版删除")
	@RequestMapping(value = "/deleteCmsTpage", method = RequestMethod.POST)
	public RestResult deleteCmsTpage(@RequestBody CmsTpageRequest cmsTpageRequest) {
		RestResult restResult = new RestResult();
		try {
			long flag = cmsTpageService.delete(cmsTpageRequest.getId());
			if (flag == 1) {
				restResult = RestResult.success(restResult);
			}
		} catch (Exception e) {
			log.error("模版删除异常",e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION);
		}
		return restResult;

	}
	

	 /** 
	 * 编辑
	 * @param id
	 * @return
	 */
	@ResponseBody
	@ApiOperation(value = "模版编辑", notes = "模版编辑")
	@RequestMapping(value = "/editCmsTpage", method = RequestMethod.GET)
	public RestResult<CmsTpageResponse> editCmsTpage(@RequestBody CmsTpageRequest cmsTpageRequest) {

		try {
			
			CmsTpageResponse cmsTpageResponse =new CmsTpageResponse();
			
		    List<CmsModuleResponse> cmsModuleResponses  = cmsModuleService.query(cmsTpageRequest.getId(), 1);
			
		    cmsTpageResponse.setCmsModules(cmsModuleResponses);
			
			return RestResult.success(cmsTpageResponse);
			
		} catch (Exception e) {
			log.error("模版编辑异常",e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION);
		}

	}
	
	

}
